﻿//Author: Nguyen The Thien Hung
// Group : 06TH1d.12
// Created : 12/5/2010
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using System.Data.Common;

namespace QUANLYNHASACH
{
    class User_BLL
    {
        public List<QUANLYNHASACH.User_td> LayDanhSachUser()
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "getUser";
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataReader reader = cmd.ExecuteReader();

                    List<QUANLYNHASACH.User_td> UsrLst = new List<QUANLYNHASACH.User_td>();

                    while (reader.Read())
                    {
                        QUANLYNHASACH.User_td usr = new QUANLYNHASACH.User_td();
                        usr.idUser = reader[0].ToString();
                        usr.idGroup = reader[1].ToString();
                        usr.Username = reader[2].ToString();
                        usr.Password = reader[3].ToString();
                        usr.HoTen = reader[4].ToString();
                        
                       
                        UsrLst.Add(usr);
                    }
                    conn.Close();
                    return UsrLst;
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi xảy ra: " + ex.Message, "Thông báo lỗi");
            }
            return null;
        }
        ///////////////////////
        public void CapNhatUser(string idUser, string idGroup, string Username, string Password, string Hoten)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "editUser";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idUser";
                    param.Value = idUser;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idGroup_new";
                    param.Value = idGroup;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@UserName_new";
                    param.Value = Username;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@Password_new";
                    param.Value = Password;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@HoTen_new";
                    param.Value = Hoten;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
        public void ThemUser(string idUser, string idGroup, string UserName, string Password, string HoTen)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "addUser";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idUser";
                    param.Value = idUser;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@idGroup";
                    param.Value = idGroup;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@UserName";
                    param.Value = UserName;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@Password";
                    param.Value = Password;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);

                    param = cmd.CreateParameter();
                    param.ParameterName = "@HoTen";
                    param.Value = HoTen;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }

        }

        public void XoaUser(string idUser)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(QUANLYNHASACH.quanlycosodulieu.GetChuoiKetNoi()))
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delUser";
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    DbParameter param = cmd.CreateParameter();
                    param.ParameterName = "@idUser";
                    param.Value = idUser;
                    param.DbType = System.Data.DbType.String;
                    cmd.Parameters.Add(param);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                };
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message, "Thông báo lỗi");
            }
        }
        public string CreateID()
        {
            SqlConnection conn = new SqlConnection(QUANLYNHASACH.connect.GetChuoiKetNoi());
            
            conn.Open();
            SqlCommand cmd = new SqlCommand("select isnull(max(cast(substring(idUser,1,4) as int)),0) from USERS", conn);
           

               int count = (int)cmd.ExecuteScalar() + 1;
               string  id= "000" + count.ToString();

               conn.Close(); 
            return id;
            
        }
    }
}
